package org.acompanhamento.domain;

import static javax.persistence.FetchType.LAZY;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

import jmine.tec.aom.domain.Property;
import jmine.tec.persist.annotation.Alias;
import jmine.tec.persist.annotation.Index;

import org.acompanhamento.CoreAppMessages;
import org.acompanhamento.dao.StatusTransictionDAO;
import org.hibernate.validator.NotNull;

import bancosys.tec.component.Documentation;
import bancosys.tec.exception.BusinessException;

@Entity
@Table(name = "WORKFLOW")
@Alias("WORKFL")
@Documentation("WORKFLOW")
@SequenceGenerator(name = "SEQ_WORKFLOW", sequenceName = "SEQ_WORKFLOW")
public class Workflow extends PropertizedEntity  {
	private Long id;

	private String name;

	private Team team;

	private List<StatusTransiction> statusTransictions;

	@Id
	@GeneratedValue(generator = "SEQ_WORKFLOW")
	@Column(name = "COD_WORKFLOW", nullable = false)
	@Documentation("WORKFLOW ID")
	public Long getId() {
		return this.id;
	}

	@NotNull
	@Index(suffix = "0")
	@Documentation("WORKFLOW NAME")
	@Column(name = "NAME", nullable = false)
	public String getName() {
		return this.name;
	}

	@Transient
	public Status getInitialStatus() {

		// XXX talvez usar a ordem da lista seja melhor
		Set<Status> to = new HashSet<Status>();
		Set<Status> from = new HashSet<Status>();

		for (StatusTransiction transiction : this.getStatusTransictions()) {
			to.add(transiction.getToStatus());
			from.add(transiction.getFromStatus());
		}

		for (Status status : from) {
			if(!to.contains(status)){
				return status;
			}
		}
		throw new BusinessException(CoreAppMessages.INITIAL_STATUS_NOT_FOUND.create());
	}

	@Transient
	public Status getNextStatus(StatusTransictionDAO statusTransictionDAO, Status from){
		List<StatusTransiction> statusTransaction = statusTransictionDAO.findByFromStatus(from);
		if(statusTransaction.size()==1){
			return statusTransaction.get(0).getToStatus();
		}
		return null;
	}



	@OneToMany
	@Index(suffix="1")
	@JoinColumn(name = "COD_WORKFLOW")
	@Documentation("WORKFLOW ID")
	public List<StatusTransiction> getStatusTransictions() {
		return this.statusTransictions;
	}

	@ManyToOne(fetch = LAZY)
	@NotNull
	@JoinColumn(name = "COD_TEAM", nullable = false)
	@Documentation("TEAM ID")
	public Team getTeam() {
		return this.team;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setStatusTransictions(List<StatusTransiction> statusTransictions) {
		this.statusTransictions = statusTransictions;
	}

	public void setTeam(Team team) {
		this.team = team;
	}

	@Override
	public String toString() {
		return this.getName();
	}

	@Override
	@Transient
	protected Set<Property> getEntityProperties() {
		return Collections.emptySet();
	}
}
